package com.zbkj.business.core;

import cn.hutool.http.HttpUtil;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class GenerateSign {
    private static final Charset UTF_8 = StandardCharsets.UTF_8;

    public static String getSignStr(Map<String, Object> params) {
        List<String> paramStr = new ArrayList<>(params.size());
        for (String key : params.keySet()) {
            paramStr.add(key + "=" + params.get(key));
        }
        Collections.sort(paramStr);
        return StringUtils.join(paramStr, "&");
    }

    public static String generateSign(String content, String algorithm, String securityKey) {
        content += securityKey;
        System.out.println("sign_str: " + content);
        MessageDigest md;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
        return bytesToHex(md.digest(content.getBytes(UTF_8)));
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        HashMap<String, Object> params = new HashMap<>(10);
        String signType = "MD5";
        String securityKey = "6e163d403ac74822e6f3bfdd01040a26f9b1f49fa3bcd52126209a3297d8807d"; // 账号的securityKey
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(); // 获取当前时间
        String currentTime = sdf.format(date); // 格式化为字符串
        params.put("user_id", 179304);
        params.put("role_id", 179304);
        params.put("version", "2.0");
        params.put("sign_type", signType);
//        params.put("adunittypes", 5);
        params.put("start_time", "2024-06-12 00:00:00");
        params.put("end_time", "2024-06-13 00:00:00");
        params.put("current_time", currentTime);
        params.put("metric", "ecpm,revenue,api_ecpm,api_revenue");


        String signStr = GenerateSign.getSignStr(params);
        System.out.println("params_str: " + signStr);


        String sign = GenerateSign.generateSign(signStr, signType, securityKey);
        System.out.println("sign: " + sign);
        params.put("sign", sign);
        AtomicReference<String> getUrl= new AtomicReference<>("https://www.csjplatform.com/union_media/open/api/v1/mediation/get_hour_report_data?");
        params.forEach((k, v) -> getUrl.set(getUrl + k + "=" + v + "&"));
        System.out.println(getUrl.get());
        String result3 = HttpUtil.get("https://www.csjplatform.com/union_media/open/api/v1/mediation/get_hour_report_data", params);
        System.out.println(result3);
    }
}
// params_str: current_time=2022-01-06 16:05:49&role_id=459&sign_type=MD5&user_id=459&vers
// sign_str: current_time=2022-01-06 16:05:49&role_id=459&sign_type=MD5&user_id=459&versio
// sign: 4d47b3a7679448c3da5c1d05e5291533
